package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Dictionary;
import cn.wolfcode.crm.service.IDictionaryService;
import cn.wolfcode.crm.util.JSONResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("dictionary")
public class DictionaryController {
    @Autowired
    public IDictionaryService dictionaryService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"dictionary:view","字典列表"},logical = Logical.OR)
    public String view() {
        return "dictionary/dictionary";
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Dictionary> selectAll() {
        return dictionaryService.selectAll();
    }

    @RequestMapping("selectDictionaryByName")
    @ResponseBody
    public List<Dictionary> selectDictionaryByName(String name) {
        return dictionaryService.selectDictionaryByName(name);
    }

    @RequestMapping("delete")
    @RequiresPermissions(value = {"dictionary:delete","字典删除"},logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id) {
        try {
            dictionaryService.deleteByPrimaryKey(id);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("删除失败");
        }
    }
    @ResponseBody
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"dictionary:saveOrUpdate","字典新增或编辑"},logical = Logical.OR)
    public JSONResult saveOrUpdate(Dictionary dictionary){
        try {
            if(dictionary.getId() != null){
                dictionaryService.updateByPrimaryKey(dictionary);
            }else{
                dictionaryService.insert(dictionary);
            }
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }
}
